package com.exam.dao;

import com.exam.domain.entity.Answer;
import com.exam.domain.entity.Question;

import java.sql.Connection;
import java.sql.ResultSet;

public class AnswerDao {

    public Answer getAnswerByRecordId(Integer recordId,Integer questionId) {
        String sql = "SELECT * FROM student_answers WHERE record_id = ? AND question_id = ?";
        // 执行查询操作，返回结果集
        // 执行查询操作，返回 ResultSet

        try (Connection connection = DBUtil.getConnection();
             ResultSet rs = DBUtil.query(connection, sql, recordId, questionId)){
            // 结果集中有数据，映射到 Question 对象
            Answer answer = new Answer();
            if (rs.next()) {
                answer.setRecordId(rs.getInt("record_id"));
                answer.setQuestionId(rs.getInt("question_id"));
                answer.setScore(rs.getInt("score"));
                answer.setSelectedOption(rs.getString("selected_option"));
                // 返回查询到的用户
                return answer;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查询问题失败", e);
        }
        // 如果没有找到用户，返回 null
        return null;
    }
}
